/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lcfgly.Dao;

import com.lcfgly.DatabaseFactory.DataFactoryInterface;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

/**
 *
 * @author lcfgly
 */
@Repository
public class CountyImpl implements Serializable {

    @Autowired
    @Qualifier("databaseshoprice")
    private DataFactoryInterface dataFactory;
    private final String sqlheader = "SELECT id,name,parentid FROM cityinfo ";

    /**
     * 获取地区编号
     *
     * @param provinceid
     * @return
     * @throws SQLException
     */
    public List findInfo(int provinceid) throws SQLException {
        String sql = sqlheader + " WHERE parentid=?";
        return dataFactory.rowsWithOutObject(sql, provinceid);
    }

    /**
     * 获取地址
     *
     * @param id
     * @return
     */
    public String GetAddress(int id) {
        String sql = sqlheader + " WHERE id=?";
        try {
            Map res = dataFactory.oneWithOutObject(sql, id);
            int parentid = Integer.valueOf(res.get("parentid").toString());
            if (parentid != 1) {
                return GetAddress(parentid) + res.get("name");
            }
            return res.get("name").toString();
        } catch (SQLException e) {
            Logger.getLogger("SqlError").error(e);
            return "";
        }
    }
    
    /**
     * 获取城市名称
     * @param id
     * @return 
     */
    public String Cityname(int id){
        String sql=sqlheader+" WHERE id=?";
        try{
            Map res=dataFactory.oneWithOutObject(sql, id);
            return res.get("name").toString();
        }catch(SQLException e){
            Logger.getLogger("SqlError").error(e);
            return "";
        }
    }

}
